package fl.ass.service.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cmm.service.core.framework.biz.BaseBizImpl;
import cmm.service.core.framework.dao.IBaseDao;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.condition.AssPrdEvaluationQueryCondition;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.AssPrdEvaluationDTO;
import fl.ass.service.dao.AssPrdEvaluationMapper;
import fl.ass.service.po.AssPrdEvaluation;
import fl.ass.service.service.IAssPrdEvaluationService;
import fl.ass.service.transform.AssPrdEvaluationTransform;

/**
 * @Description: 资产评估 服务层接口实现
 * @Description: 类中已经实现了简单增删改查操作,可以直接使用,如果有需要重写该方法即可：
 * @Description: 1、add(TVO entity) 2、removeById(Long id) 3、modify(DTO entity) 4、selectById(Long id)	5、searchListPage(Pager page) 7、searchList(DTO dto)   
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssPrdEvaluationServiceImpl extends BaseBizImpl<AssPrdEvaluation, AssPrdEvaluationDTO, AssPrdEvaluationQueryCondition> implements IAssPrdEvaluationService {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IAssPrdEvaluationService.class);
	
	@Autowired
	private AssPrdEvaluationMapper assPrdEvaluationMapper;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Override
	public List<AssPrdEvaluationDTO> searchList(AssPrdEvaluationQueryCondition condition) throws BaseException {
		if(null == condition) return new ArrayList<AssPrdEvaluationDTO>();
		List<AssPrdEvaluation> list = this.assPrdEvaluationMapper.selectList(condition);
		return AssPrdEvaluationTransform.toDTOList(list);
	}
	
	@Override
	public IBaseDao<AssPrdEvaluation,AssPrdEvaluationQueryCondition> getDao() {
		return this.assPrdEvaluationMapper;
	}

	/* (non-Javadoc)
	 * @see fl.ass.service.service.IAssPrdEvaluationService#findAssPrdEvaluationListByPage(cmm.mid.core.framework.page.Pager)
	 */
	@Override
	public Pager findAssPrdEvaluationListByPage(Pager page) {
		List<AssPrdEvaluationDTO> list = this.assPrdEvaluationMapper.findAssPrdEvaluationListByPage(page);
		if(!CollectionUtils.isNotEmpty(list)){
			list = new ArrayList<AssPrdEvaluationDTO>();
		}
		SysDictionaryDTO dictionary = null;
		Long dealAmt = null;
		Long zero = new Long(0);
		for(AssPrdEvaluationDTO dto : list){
			//处理拖车状态
			if(StringUtils.isNotBlank(dto.getBackStsCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.BACK_STATUS_CD, dto.getBackStsCd());
				if(null != dictionary){
					dto.setBackStsCd(dictionary.getCodeNm());
				}
			}
			//处理处置状态
			if(StringUtils.isNotBlank(dto.getDealStsCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.DEAL_STATUS_CD, dto.getDealStsCd());
				if(null != dictionary){
					dto.setDealStsCd(dictionary.getCodeNm());
				}
			}
			//处理评价状态
			if(StringUtils.isNotBlank(dto.getEvalStatusCd())){
				dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.EVAL_STATUS_CD, dto.getEvalStatusCd());
				if(null != dictionary){
					dto.setEvalStatusCd(dictionary.getCodeNm());
				}
			}
			//处理处置金额
			dealAmt = dto.getDealAmt();
			if(null == dealAmt){
				dealAmt = dto.getSellAmt();
				if(null != dealAmt && dealAmt.compareTo(zero) > 0){
					dto.setDealAmt(dealAmt);
				}else{
					dto.setDealAmt(zero);
				}
			}else{
				if(dealAmt.compareTo(zero) < 1){
					dto.setDealAmt(zero);
				}
			}
		}
		page.setResultList(list);
		return page;
	}
	
}

